package edu.compila.client;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

import edu.compila.client.api.Tela;

public class SobreTela extends Composite {
	private static final String REQUEST_URL = "/sobre.html"; 

	private static final String TOKEN = "sobre";
	public static Tela init() {
		return new Tela(TOKEN) {
			@Override
			protected Widget createWidget() {
				return new SobreTela();
			}
		};
	}

	private DockPanel dockPanel;
	private Caixa caixa;
	private FlexTable flexTable;
	private Image image;
	private HTML html;
	private Image image_1;


	public SobreTela() {
		createContents();
	}
	private void createContents() {
		{
			dockPanel = new DockPanel();
			initWidget(dockPanel);
			dockPanel.setSize("100%", "100%");
		}
		{
			caixa = new Caixa();
			dockPanel.add(caixa, DockPanel.CENTER);
			dockPanel.setCellHeight(caixa, "100%");
			dockPanel.setCellWidth(caixa, "100%");
		}
		{
			flexTable = new FlexTable();
			dockPanel.add(flexTable, DockPanel.NORTH);
		}
		{
			image = new Image("voltar.png");
			image.setTitle("Voltar ao teste.");
			image.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					History.newItem("teste");
				}
			});
			{
				image_1 = new Image("logo.jpg");
				flexTable.setWidget(0, 0, image_1);
				flexTable.getCellFormatter().setWidth(0, 0, "100%");
			}
			image.setStyleName("gwt-cursor-pointer");
			flexTable.setWidget(0, 1, image);
			flexTable.getCellFormatter().setWidth(0, 1, "");
		}
		flexTable.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_RIGHT);
		{
			html = new HTML("<center>\r\n\t<img src=\"carregando.gif\">\r\n</center>", true);
			html.setStyleName("gwt-Sobre");
			caixa.getMain().setWidget(html);
			html.setSize("100%", "100%");
		}
		{
			RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(REQUEST_URL));
			try {
			  @SuppressWarnings("unused")
			Request request = builder.sendRequest(null, new RequestCallback() {
				  @Override
			    public void onError(Request request, Throwable exception) {
			    	  html.setHTML("Ocorreu um ero, por favor entre em contato e reporte.");
			    }

				@Override
				public void onResponseReceived(Request request, Response response) {
				      if (200 == response.getStatusCode()) {
				    	  html.setHTML(response.getText());
				      } else {
				    	  html.setHTML("Ocorreu um ero, por favor entre em contato e reporte.");
				      }
				}

			  });
			} catch (RequestException e) {
		    	  html.setHTML("Ocorreu um ero, por favor entre em contato e reporte.");
			}

		}

		flexTable.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
	}

}
